package base.common;

import com.alibaba.fastjson.JSON;

import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

public class PersonComparable implements Comparable<PersonComparable>{
    private Integer count=0;
    private String name;

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PersonComparable that = (PersonComparable) o;
        return Objects.equals(count, that.count) &&
                Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(count, name);
    }

    @Override
    public int compareTo(PersonComparable o) {
        return this.count-o.count;
    }

    public static void main(String[] args) {
        PersonComparable p3=new PersonComparable();
        p3.setCount(3);
        p3.setName("c");

        PersonComparable p1=new PersonComparable();
        p1.setCount(1);
        p1.setName("a");

        PersonComparable p2=new PersonComparable();
        p2.setCount(2);
        p2.setName("b");

        PersonComparable p4=new PersonComparable();
        p4.setCount(4);
        p4.setName("d");

        Map<PersonComparable,String> map=new TreeMap();
        map.put(p1,"a");
        map.put(p2,"b");
        map.put(p3,"c");
        map.put(p4,"d");

        System.out.println(JSON.toJSONString(map));

    }
}
